package com.koron.order.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.util.message.MessageUtil;
import com.koron.order.common.bean.po.OrderReassignPo;
import com.koron.order.common.bean.vo.OrderReassignVo;
import com.koron.order.common.mapper.OrderReassignMapper;
import com.koron.order.common.service.OrderReassignService;
import com.koron.order.common.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (OrderReassign)表服务实现类
 *
 * @author zhouj
 * @since 2022-03-22 11:49:41
 */
@Slf4j
@Service("orderReassignService")
public class OrderReassignServiceImpl implements OrderReassignService {

    @Autowired
    MessageUtil messageUtil;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, OrderReassignPo orderReassign) {

        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);

        return mapper.insertOrUpdate(orderReassign);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<OrderReassignPo> orderReassignList) {

        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);

        return mapper.insertOrUpdateBatch(orderReassignList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderReassignPo orderReassign) {

        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);

        return mapper.insert(orderReassign);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<OrderReassignPo> orderReassignList) {

        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);

        return mapper.insertBatch(orderReassignList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderReassignPo orderReassign) {
        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);
        return mapper.update(orderReassign);
    }

    @Override
    public int updateByOrderId(SessionFactory factory, OrderReassignPo orderReassign) {
        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);
        return mapper.updateByOrderId(orderReassign);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderReassignPo queryById(SessionFactory factory, String id) {

        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryLatestByOrderId")
    public OrderReassignPo queryLatestByOrderId(SessionFactory factory, String orderId) {

        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);

        return mapper.queryLatestByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OrderReassignPo> queryList(SessionFactory factory, OrderReassignPo orderReassign) {

        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);

        List<OrderReassignPo> list = mapper.queryList(orderReassign);

        return list;
    }

    @Override
    @TaskAnnotation("notifyDispatch")
    public Integer notifyDispatch(SessionFactory factory, String currDs) {

        //先查询出来有哪些工单存在转派负责人,多次取最新一次创建时间的
        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);

        List<OrderReassignVo> list = mapper.queryNotifyDispatch();

        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        Date date = new Date();

        List<OrderReassignVo> filterList = list.stream().filter(p -> onTime(p.getCreateTime(), date)).collect(Collectors.toList());

        for (int i = 0; i < filterList.size(); i++) {

            OrderReassignVo orderReassignVo = filterList.get(i);

            sendMessage(orderReassignVo.getWorker(), orderReassignVo);

        }

        return filterList.size();
    }

    private void sendMessage(String handler, OrderReassignVo orderReassignVo) {
        String qywxHref = messageUtil.generateQywxHref("order_reassign", orderReassignVo.getOrderId());
        messageUtil.sendMessageWithTemplate("order_reassign", qywxHref, handler, orderReassignVo.getCreateByName(), orderReassignVo.getTitle());
    }

    public Boolean onTime(Date start, Date end) {

        try {
            int days = DateUtils.getDays(start, end);
            if (days > 0 && days % 7 == 0) {
                return true;
            }

        } catch (ParseException e) {
            log.error(e.getMessage());
        }
        return false;

    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, OrderReassignPo orderReassign, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        OrderReassignMapper mapper = factory.getMapper(OrderReassignMapper.class);

        List<OrderReassignPo> list = mapper.queryList(orderReassign);

        return new PageInfo(list);
    }

}
